//
//  FWEditGoodsTagView.m
//  FengWoFilterTickerDemo
//
//  Created by 郝秀丽 on 15/10/29.
//  Copyright © 2015年 FengWo. All rights reserved.
//

#import "FWEditGoodsTagView.h"

static const CGFloat kLabelFontSize = 12.f;
static const CGFloat kLeftMargin = 30.f;
static const CGFloat kTopMargin = 25.f;
static const CGFloat kWidthL = 50.f;
static const CGFloat kWidthR = 200.f;
static const CGFloat kHeight = 30.f;
static const CGFloat kMiddleMargin = 10.f;

@interface FWEditGoodsTagView ()

@property (nonatomic, strong) Model *model;
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) UITextField *brandTextField;
@property (nonatomic, strong) UITextField *productNameTextField;
@property (nonatomic, strong) UITextField *moneyTypeTextField;
@property (nonatomic, strong) UITextField *priceTextField;
@property (nonatomic, strong) UITextField *countryTextField;
@property (nonatomic, strong) UITextField *buyAddressTextField;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *finishButton;
@property (nonatomic, strong) UIButton *deleteButton;

@end


@implementation FWEditGoodsTagView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self buildMaskView];
        [self buildBrandTextField];
        [self buildProductNameTextField];
        [self buildMoneyTypeTextField];
        [self buildPriceTextField];
        [self buildCountryTextField];
        [self buildBuyAddressTextField];
        [self buildCancelButton];
        [self buildFinishButton];
        [self buildDeleteButton];
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.maskView.frame = self.bounds;
    self.brandTextField.frame = CGRectMake(kLeftMargin, 100, kWidthL, kHeight);
    self.productNameTextField.frame = CGRectMake(CGRectGetMaxX(self.brandTextField.frame) + kMiddleMargin, self.brandTextField.frame.origin.y, kWidthR, kHeight);
    self.moneyTypeTextField.frame = CGRectMake(kLeftMargin, CGRectGetMaxY(self.brandTextField.frame) + kTopMargin, kWidthL, kHeight);
    self.priceTextField.frame = CGRectMake(CGRectGetMaxX(self.moneyTypeTextField.frame) + kMiddleMargin, self.moneyTypeTextField.frame.origin.y, kWidthR, kHeight);
    self.countryTextField.frame = CGRectMake(kLeftMargin, CGRectGetMaxY(self.moneyTypeTextField.frame) + kTopMargin, kWidthL, kHeight);
    self.buyAddressTextField.frame = CGRectMake(CGRectGetMaxX(self.countryTextField.frame) + kMiddleMargin, self.countryTextField.frame.origin.y, kWidthR, kHeight);
    
    self.cancelButton.frame = CGRectMake(kLeftMargin, CGRectGetMaxY(self.countryTextField.frame) + kTopMargin, kWidthL, kHeight);
    self.finishButton.frame = CGRectMake(CGRectGetMaxX(self.cancelButton.frame) + kMiddleMargin, self.cancelButton.frame.origin.y, kWidthL, kHeight);
    self.deleteButton.frame = CGRectMake(CGRectGetMaxX(self.finishButton.frame) + kMiddleMargin, self.cancelButton.frame.origin.y, kWidthL, kHeight);
    
}

#pragma mark - Public
- (void)reloadEditGoodsTagViewWithModel:(Model *)model {
    self.model = model;
    
    self.brandTextField.text = model.brand;
    self.productNameTextField.text = model.productName;
    self.moneyTypeTextField.text = model.moneyType;
    self.priceTextField.text = model.price;
    self.countryTextField.text = model.country;
    self.buyAddressTextField.text = model.buyAddress;

}

#pragma mark - Setter
- (void)setType:(FWEditGoodsTagViewType)type {
    _type = type;
    if (type == FWEditGoodsTagViewTypeAdd) {
        self.deleteButton.hidden = YES;
    } else {
        self.deleteButton.hidden = NO;
    }
}
#pragma mark - Builder
- (void)buildMaskView {
    self.maskView = [[UIView alloc] init];
    self.maskView.backgroundColor = [UIColor blackColor];
    self.maskView.alpha = 0.8f;
    [self addSubview:self.maskView];
}

- (void)buildBrandTextField {
    self.brandTextField = [[UITextField alloc] init];
    self.brandTextField.backgroundColor = [UIColor clearColor];
    self.brandTextField.layer.borderWidth = 1.f;
    self.brandTextField.layer.borderColor = [UIColor whiteColor].CGColor;
    self.brandTextField.textAlignment = NSTextAlignmentLeft;
    self.brandTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"品牌" attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.f green:1.f blue:1.f alpha:0.5f]}];
    self.brandTextField.textColor = [UIColor whiteColor];
    self.brandTextField.tintColor = [UIColor whiteColor];
    self.brandTextField.font = [UIFont systemFontOfSize:kLabelFontSize];
    [self addSubview:self.brandTextField];
}

- (void)buildProductNameTextField {
    self.productNameTextField = [[UITextField alloc] init];
    self.productNameTextField.backgroundColor = [UIColor clearColor];
    self.productNameTextField.layer.borderWidth = 1.f;
    self.productNameTextField.layer.borderColor = [UIColor whiteColor].CGColor;
    self.productNameTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"商品名" attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.f green:1.f blue:1.f alpha:0.5f]}];
    self.productNameTextField.textColor = [UIColor whiteColor];
    self.productNameTextField.tintColor = [UIColor whiteColor];
    self.productNameTextField.textAlignment = NSTextAlignmentLeft;
    self.productNameTextField.font = [UIFont systemFontOfSize:kLabelFontSize];
    [self addSubview:self.productNameTextField];
}

- (void)buildMoneyTypeTextField {
    self.moneyTypeTextField = [[UITextField alloc] init];
    self.moneyTypeTextField.backgroundColor = [UIColor clearColor];
    self.moneyTypeTextField.layer.borderWidth = 1.f;
    self.moneyTypeTextField.layer.borderColor = [UIColor whiteColor].CGColor;
    self.moneyTypeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"币种" attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.f green:1.f blue:1.f alpha:0.5f]}];
    self.moneyTypeTextField.textColor = [UIColor whiteColor];
    self.moneyTypeTextField.tintColor = [UIColor whiteColor];
    self.moneyTypeTextField.textAlignment = NSTextAlignmentLeft;
    self.moneyTypeTextField.font = [UIFont systemFontOfSize:kLabelFontSize];
    [self addSubview:self.moneyTypeTextField];
}

- (void)buildPriceTextField {
    self.priceTextField = [[UITextField alloc] init];
    self.priceTextField.backgroundColor = [UIColor clearColor];
    self.priceTextField.layer.borderWidth = 1.f;
    self.priceTextField.layer.borderColor = [UIColor whiteColor].CGColor;
    self.priceTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"价格" attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.f green:1.f blue:1.f alpha:0.5f]}];
    self.priceTextField.textColor = [UIColor whiteColor];
    self.priceTextField.tintColor = [UIColor whiteColor];
    self.priceTextField.textAlignment = NSTextAlignmentLeft;
    self.priceTextField.font = [UIFont systemFontOfSize:kLabelFontSize];
    [self addSubview:self.priceTextField];
}

- (void)buildCountryTextField {
    self.countryTextField = [[UITextField alloc] init];
    self.countryTextField.backgroundColor = [UIColor clearColor];
    self.countryTextField.layer.borderWidth = 1.f;
    self.countryTextField.layer.borderColor = [UIColor whiteColor].CGColor;
    self.countryTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"国家/..." attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.f green:1.f blue:1.f alpha:0.5f]}];
    self.countryTextField.textColor = [UIColor whiteColor];
    self.countryTextField.tintColor = [UIColor whiteColor];
    self.countryTextField.textAlignment = NSTextAlignmentLeft;
    self.countryTextField.font = [UIFont systemFontOfSize:kLabelFontSize];
    [self addSubview:self.countryTextField];
}

- (void)buildBuyAddressTextField {
    self.buyAddressTextField = [[UITextField alloc] init];
    self.buyAddressTextField.backgroundColor = [UIColor clearColor];
    self.buyAddressTextField.layer.borderWidth = 1.f;
    self.buyAddressTextField.layer.borderColor = [UIColor whiteColor].CGColor;
    self.buyAddressTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"具体购买地址" attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.f green:1.f blue:1.f alpha:0.5f]}];
    self.buyAddressTextField.textColor = [UIColor whiteColor];
    self.buyAddressTextField.tintColor = [UIColor whiteColor];
    self.buyAddressTextField.textAlignment = NSTextAlignmentLeft;
    self.buyAddressTextField.font = [UIFont systemFontOfSize:kLabelFontSize];
    [self addSubview:self.buyAddressTextField];
}

- (void)buildCancelButton {
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelButton.backgroundColor = [UIColor whiteColor];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(didClickCancelButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.cancelButton];
}

- (void)buildFinishButton {
    self.finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.finishButton.backgroundColor = [UIColor whiteColor];
    [self.finishButton setTitle:@"完成" forState:UIControlStateNormal];
    [self.finishButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.finishButton addTarget:self action:@selector(didClickFinishButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.finishButton];
}

- (void)buildDeleteButton {
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.deleteButton.backgroundColor = [UIColor whiteColor];
    [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [self.deleteButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(didClickDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.deleteButton];
}


#pragma mark - SelectorMethod
- (void)didClickCancelButton:(UIButton *)button {
    [self removeFromSuperview];
    if (self.delegate && [self.delegate respondsToSelector:@selector(editGoodsTagView:didClickCancelButtonWithModel:)]) {
        [self.delegate editGoodsTagView:self didClickCancelButtonWithModel:self.model];
    }
}

- (void)didClickFinishButton:(UIButton *)button {
    self.model = [[Model alloc] init];
    self.model.brand = self.brandTextField.text;
    self.model.productName = self.productNameTextField.text;
    self.model.moneyType = self.moneyTypeTextField.text;
    self.model.price = self.priceTextField.text;
    self.model.country = self.countryTextField.text;
    self.model.buyAddress = self.buyAddressTextField.text;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(editGoodsTagView:didClickFinishButtonWithModel:type:)]) {
        [self.delegate editGoodsTagView:self didClickFinishButtonWithModel:self.model type:self.type];
    }
    [self removeFromSuperview];

}

- (void)didClickDeleteButton:(UIButton *)button {
    [self removeFromSuperview];
    if (self.delegate && [self.delegate respondsToSelector:@selector(editGoodsTagView:didClickDeleteButtonWithModel:)]) {
        [self.delegate editGoodsTagView:self didClickDeleteButtonWithModel:self.model];
    }
}


@end
